<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目id" prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          placeholder="请输入项目id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申报人id" prop="applicantId">
        <el-input
          v-model="queryParams.applicantId"
          placeholder="请输入申报人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申报单位" prop="applicationUnit">
        <el-input
          v-model="queryParams.applicationUnit"
          placeholder="请输入申报单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人id" prop="approverId">
        <el-input
          v-model="queryParams.approverId"
          placeholder="请输入审核人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:application:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:application:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:application:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:application:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applicationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目id" align="center" prop="projectId" />
      <el-table-column label="申报人id" align="center" prop="applicantId" />
      <el-table-column label="申报单位" align="center" prop="applicationUnit" />
      <el-table-column label="申报日期" align="center" prop="applicationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applicationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="经费来源" align="center" prop="fundSource" />
      <el-table-column label="申文路径" align="center" prop="applicationFilePath" />
      <el-table-column label="附件路径" align="center" prop="attachmentPath" />
      <el-table-column label="申报状态" align="center" prop="applicationStatus" />
      <el-table-column label="团队成员" align="center" prop="teamMembers" />
      <el-table-column label="审核人id" align="center" prop="approverId" />
      <el-table-column label="审核意见" align="center" prop="approvalComments" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:application:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:application:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改申报项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目名称" prop="projectId">
          <el-select v-model="form.projectId" placeholder="请选项目名称">
            <el-option
              v-for="item in projectBuilded"
              :key="item.projectId"
              :label="item.projectName"
              :value="item.projectId"
              >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申报人id" prop="applicantId">
          <el-input v-model="form.applicantId" placeholder="请输入申报人id" />
        </el-form-item>
        <el-form-item label="申报单位" prop="applicationUnit">
          <el-input v-model="form.applicationUnit" placeholder="请输入申报单位" />
        </el-form-item>
        <el-form-item label="申报日期" prop="applicationDate">
          <el-date-picker clearable
            v-model="form.applicationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申报日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="经费来源" prop="fundSource">
          <el-input v-model="form.fundSource" placeholder="请输入经费来源" />
        </el-form-item>
        <el-form-item label="申文路径" prop="applicationFilePath">
          <el-input v-model="form.applicationFilePath" placeholder="请输入申文路径" />
        </el-form-item>
        <el-form-item label="附件路径" prop="attachmentPath">
          <el-input v-model="form.attachmentPath" placeholder="请输入附件路径" />
        </el-form-item>
        <el-form-item label="团队成员" prop="teamMembers">
          <el-input v-model="form.teamMembers" placeholder="请输入团队成员" />
        </el-form-item>
        <el-form-item label="审核人id" prop="approverId">
          <el-input v-model="form.approverId" placeholder="请输入审核人id" />
        </el-form-item>
        <el-form-item label="审核意见" prop="approvalComments">
          <el-input v-model="form.approvalComments" placeholder="请输入审核意见" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApplication, getApplication, delApplication, addApplication, updateApplication } from "@/api/project/application";
import { listResearch} from "@/api/project/research";

export default {
  name: "Application",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 申报项目表格数据
      applicationList: [],
      // 立项完成项目列表
      projectBuilded: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        applicantId: null,
        applicationUnit: null,
        applicationDate: null,
        fundSource: null,
        applicationFilePath: null,
        attachmentPath: null,
        applicationStatus: null,
        teamMembers: null,
        approverId: null,
        approvalComments: null,
        createdBy: null,
        createdAt: null,
        updatedBy: null,
        updatedAt: null,
        deletedBy: null,
        deletedAt: null,
        isDeleted: null,
        version: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectId: [
          { required: true, message: "项目id不能为空", trigger: "blur" }
        ],
        applicantId: [
          { required: true, message: "申报人id不能为空", trigger: "blur" }
        ],
        applicationUnit: [
          { required: true, message: "申报单位不能为空", trigger: "blur" }
        ],
        applicationDate: [
          { required: true, message: "申报日期不能为空", trigger: "blur" }
        ],
        createdBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询申报项目列表 */
    getList() {
      this.loading = true;
      listApplication(this.queryParams).then(response => {
        this.applicationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        applicationId: null,
        projectId: null,
        applicantId: null,
        applicationUnit: null,
        applicationDate: null,
        fundSource: null,
        applicationFilePath: null,
        attachmentPath: null,
        applicationStatus: null,
        teamMembers: null,
        approverId: null,
        approvalComments: null,
        createdBy: null,
        createdAt: null,
        updatedBy: null,
        updatedAt: null,
        deletedBy: null,
        deletedAt: null,
        isDeleted: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.applicationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.handleBuilded();
      this.open = true;
      this.title = "添加申报项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const applicationId = row.applicationId || this.ids
      getApplication(applicationId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改申报项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.applicationId != null) {
            updateApplication(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApplication(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const applicationIds = row.applicationId || this.ids;
      this.$modal.confirm('是否确认删除申报项目编号为"' + applicationIds + '"的数据项？').then(function() {
        return delApplication(applicationIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/application/export', {
        ...this.queryParams
      }, `application_${new Date().getTime()}.xlsx`)
    },
    /** 获取立项完成项目 */
    handleBuilded(){
      listResearch({pageSize:100,pageNum:10}).then(response => {
          this.projectBuilded=response.rows.filter(row => row.approvalStatus=== 2);
          console.log(response.rows);
          console.log(this.projectBuilded);
      })
    }
  }
};
</script>
